package umacis.core;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

import systemviews.OutlineView;
import umacis.data.ActionClasses;
import umacis.data.Bindings;
import umacis.data.description.ApplicationDescription;
import umacis.data.description.SystemDescription;
import umacis.data.elements.Action;
import umacis.data.elements.Binding;
import umacis.data.elements.Key;
import umacis.data.elements.Menu;
import umacis.data.view.SystemView;

import com.sun.lwuit.Display;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Log;
import com.sun.lwuit.util.Resources;



/**
 * @author Simone
 *
 */
public class Umacis  {
	public Umacis() {
		
	}
	
	private static Umacis INSTANCE = new Umacis();
	private final static String RECORDSTORENAME = "recordstore";
	public static Umacis getInstance() {
		return INSTANCE;
	}
	
	private Vector implementationClassNames = new Vector();
	private Vector lwuitResourceFileNames = new Vector();
	private javax.microedition.lcdui.Display lcduiDisplay;
	private RecordStore recordStore;
	private String currentLwuitResourceFileName;
	
	private ApplicationDescription applicationDescription = new ApplicationDescription();
	private Application application;
	
	private SystemDescription systemDescription;
	private Systeem system;
	private ActionClassManager actionClassManager;
	
	private final static String[] systemViewClasses = {
		OutlineView.class.getName(),
//		EditKeyBindingsView.classgetName(),
//		AddComponentView.class,getName(),
//		EditPropertiesView.class,getName(),
	};
	//getters and setters**************************************
	
	Bindings bindings = new Bindings();
	
	public Application getApplication() {
		return application;
	}

	
	
	//***********************init*********************************
	public void start(MIDlet midlet) {	
		Display.init(midlet); //	init LWUIT
		lcduiDisplay = javax.microedition.lcdui.Display.getDisplay(midlet);//	get the  javax.microedition.lcdui.Display  to show non lwuit screens  
		readConfigTxtFile(implementationClassNames, lwuitResourceFileNames);
		recordStore = openRecordStore(RECORDSTORENAME);
		Log.p("Recordstore opened: <" + RECORDSTORENAME +">", Log.INFO);
		applicationDescription.loadFrom(recordStore); //persisted from last session
		currentLwuitResourceFileName = applicationDescription.getResourceFileName();
		loadLwuitResourceFile("LWUITtheme.res"); //currentLwuitResourceFileName); //default look for all components.
		
		application = new Application(applicationDescription);
		
		actionClassManager = ActionClassManager.getInstance();
				
		system = new Systeem(systemViewClasses, actionClassManager.getOffset());
		
		actionClassManager.addClasses(system.getSystemViewDescriptions());
		
		actionClassManager.addClasses(implementationClassNames);
			
		setUmacisBindings();
		new EventListener();
		
		application.show();
		
		
		
	}
	
	public void setUmacisBindings() {
		new Binding(new int[] {Key.ONE, Key.NORMAL}, new Action(OutlineView.class.getName(), (String)null));
		//new Binding(new int[] {Key.ONE, Key.NORMAL}, new Action(new Menu()));

	}

	private RecordStore openRecordStore(String recordStoreName) {
		try {
			return RecordStore.openRecordStore(recordStoreName, true);
		} catch (RecordStoreFullException e) {
			Log.p("RecordStoreFullException" + e.getMessage(), Log.ERROR);
			
		} catch (RecordStoreNotFoundException e) {
			Log.p("RecordStoreNotFoundException" + e.getMessage(), Log.ERROR);
			
		} catch (RecordStoreException e) {
			Log.p("RecordStoreException" + e.getMessage(), Log.ERROR);
		}
		return null;
	}

	private boolean loadLwuitResourceFile(String fileName) {
		Resources r = null;
        try {
			r =  Resources.open("/" + fileName);
			UIManager.getInstance().setThemeProps(r.getTheme(
					r.getThemeResourceNames()[0]));
			Log.p("LWUIT resource file loaded: <" + fileName +">", Log.INFO);
		} catch (IOException e) {
			Log.p("No resource file in the jar file named: <" + fileName +">", Log.WARNING);
			return false;
		}
		return true;
	}
	
	/**
	 * read in the classpaths and names of implementation classes and LWUIT 
	 * resource file names included in the Jar file from config.txt also included
	 * @param implementationClassNames
	 * @param lwuitResourceFileNames
	 */
	private void readConfigTxtFile(Vector implementationClassNames,
			Vector lwuitResourceFileNames) {
		// TODO Auto-generated method stub
		
	}

	public ApplicationDescription getApplicationDescription() {
		return applicationDescription;
	}



	public void add(Binding binding) {
		bindings.put(binding);
	}



	public Bindings getBindings() {
		return bindings;
	}
	
	//*********************end init
	

	
	///*misc little helper methods******************************************
	

	
	

}
